package org.dawfinn.droiditarian;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dawfinn.droiditarian.server.data.Deed;
import org.dawfinn.droiditarian.server.data.PMF;

import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class DeedCompleteServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		String key = req.getParameter("id");
		// add one to completed if this is completed, or subtract one if un-completed
		boolean completed = req.getParameter("complete").equals("" + true);
		int deltaComplete = completed ? 1 : -1;

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Deed d = pm.getObjectById(Deed.class, KeyFactory.stringToKey(key));
		Transaction tx = pm.currentTransaction();
		tx.begin();
		d.setCompletedCount(d.getCompletedCount() + deltaComplete);
		tx.commit();
		pm.close();
	}
}
